<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$sqlSel = "SELECT s.*, m.cod_mod_rque, m.descr AS m_descr FROM rgl_mod_rque_sez s "
	." INNER JOIN rgl_mod_rque m ON m.id_mod_rque = s.id_mod_rque" .
		" WHERE id_sez_rque = ".intval(reqVar('id'));
$objDB->query($sqlSel,'selRec');
if (!$acSez = $objDB->fetch('selRec')) {
	backOnErr("var. id. '".reqVar('id')."' mancante o errato");
}
$_REQUEST = array_merge($acSez,$_REQUEST);

$aCamForm = array();
require("_inc/const.campiSezRque.php");

if (reqVar('azione') == 'salva') {
	$msgPag = modSez($aCamForm);
} 
$sCmdPagR = '';
if (true) {
	$sCmdPagR .= "&nbsp;&nbsp;&nbsp;";
	$sCmdPagR .= "<a class='cmd1' href=\"javascript:submAz('salva');\" >salva</a>";
}
$sCmdPagR .= cmdTorna();

testaHtml('Gestione modelli',$msgPag,'modl');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
  	function confElim() {
  		if (confirm("Confermi l'eliminazione ?")) {
  			document.location.href = "<?=$_SERVER['PHP_SELF']."?id=".reqVar('id')."&azione=elimina&".$sXSNQS?>";
  		}
  	}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdPagR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Sezione questionario</td>
   </tr>
   <tr>
      <td class="lab1" colspan='4' align='left'>Modello</td>
      <td class="txt1" colspan='4' align='left'><?=$acSez['cod_mod_rque']?></td>
      <td class="txt1" colspan='12' align='left'><?=$acSez['m_descr']?></td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         <? echo $sCmdPagR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function modSez($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_sez_rque'] = strtoupper(trim(reqVar('cod_sez_rque')));
	if (reqVar('cod_sez_rque')=='') {
		$msg .= _t("Codice obbligatorio")."<br>";
	}
	if (preg_match("/[^_A-Z0-9]/",reqVar('cod_sez_rque'))) {
		$msg = _t("Codice '%s' puo' contenere solo lettere numeri e '-'",reqVar('cod_sez_rque'))."<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	$sqlSel = "SELECT id_sez_rque FROM rgl_mod_rque_sez " .
			" WHERE id_mod_rque = ".reqVar('id_mod_rque')
			." AND cod_sez_rque = '".reqVarAS('cod_sez_rque')."'"
			." AND id_sez_rque <> ".reqVar('id');
	$objDB->exec($sqlSel);
	$aRow = $objDB->fetch();
	if ($aRow) {
		$msg .= _t("Codice gia' esistente")."<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "UPDATE rgl_mod_rque_sez SET cod_sez_rque = '".reqVarAS('cod_sez_rque')."'";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			if ($aCX[0]=='cod_sez_rque') continue;
			$sSql .= ", ".$aCX[0]." = '".reqVarAS($aCX[0])."' ";
		}
	}
	$sSql .= " WHERE id_sez_rque = ".reqvar('id');
	$objDB->exec($sSql,'upRec');
	
	header("Location: ".urlTorna("Sezione '".reqVar('cod_sez_rque')."' modificata"));
	exit();
}
